﻿using System;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace Viettel.VOFFICE.util
{
    public static class Utility
    {
        /// <summary>
        /// Lấy phần mở rộng của file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFileExtension(this string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return string.Empty;
            }
            var split = fileName.Split('.');
            if (split.Length > 0)
            {
                return string.Format(".{0}", split[split.Length - 1]);
            }
            return string.Empty;
        }

        /// <summary>
        /// T to json string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string ToJsonString<T>(this T list)
        {
            return JsonConvert.SerializeObject(list);
        }

        /// <summary>
        /// json string to type T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T ToTypeArray<T>(this string json)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        /// <summary>
        /// convert text
        /// </summary>
        /// <param name="accent"></param>
        /// <returns></returns>
        public static string NoAccent(string accent)
        {
            Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = accent.Normalize(NormalizationForm.FormD);
            return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        /// <summary>
        /// Ghi đối tượng vào tệp dưới dạng xml
        /// </summary>
        /// <param name="xmlObject"></param>
        /// <param name="filePath"></param>
        public static void WriteXmlToFile<T>(object xmlObject, string filePath)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (TextWriter textWriter = new StreamWriter(filePath))
            {
                serializer.Serialize(textWriter, xmlObject);
                textWriter.Close();//ko cần nhưng an toàn
            }
        }

        /// <summary>
        /// Lấy xml từ file và đưa vào đối tượng T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T DeserializeFileToXml<T>(string filePath)
        {
            var deserializer = new XmlSerializer(typeof(T));
            using (TextReader textReader = new StreamReader(filePath))
            {
                var result = (T)deserializer.Deserialize(textReader);
                textReader.Close();//không cần nhưng an toàn
                return result;
            }
        }
    }
}